import time
from datetime import datetime

from pymysql import NULL

from com.demo1.mysql.mysql_client import DBHelper


class PrivateDoctorRightBoundMapper:
    def save(data):
        """
        保存数据
        :param data:
        :return:
        """
        if not data:
            return
        # 获取当前日期时间
        if data.get("id") is None:
            sql = f""" insert into private_doctor_right_bound (user_id,
                                                person_id,
                                                group_id,
                                                doctor_id,
                                                wx_group_id,
                                                status,
                                                scenario_code,
                                                service_type,
                                                service_code,
                                                interests_id,
                                                source_interests_id,
                                                expire_time,
                                                gmt_created,
                                                gmt_modified)
        values ({data.get('userId')},
                {data.get('personId')},
                {data.get('groupId')},
                {data.get('doctorId')},
                {data.get('wxGroupId', NULL)},   
                {data.get('status')},
                {data.get('scenarioCode')},
                '{data.get('serviceType')}',
                '{data.get('serviceCode')}',
                {data.get('interestsId')},
                {data.get('sourceInterestsId', NULL)},
                '{data.get('expireTime')}',
                NOW(),
                NOW())"""
            return DBHelper().insert(sql, [])
        else:
            sql = "update private_doctor_right_bound  set"
            if data.get("status") is not None:
                sql += f" status = {data.get('status')} ,"
            if data.get("doctorId") is not None:
                sql += f" doctor_id = {data.get('doctorId')} ,"
            if data.get("personId") is not None:
                sql += f" person_id = {data.get('personId')} ,"
            if data.get("groupId") is not None:
                sql += f" group_id = '{data.get('groupId')}' ,"
            if data.get("weGroupId") is not None:
                sql += f" wx_group_id = '{data.get('wxGroupId')}' ,"
            if data.get("expireTime") is not None:
                sql += f" expire_time = '{data.get('expireTime')}' ,"
            if data.get("scenarioCode") is not None:
                sql += f" scenario_code = '{data.get('scenarioCode')}' ,"
            if data.get("serviceType") is not None:
                sql += f" service_type = '{data.get('serviceType')}' ,"
            if data.get("serviceCode") is not None:
                sql += f" service_code = '{data.get('serviceCode')}' ,"
            if data.get("sourceInterestsId") is not None:
                sql += f" source_interests_id = '{data.get('sourceInterestsId')}' ,"
            sql += " gmt_modified = NOW() where id = %s"

            # mysql_client.update(sql, (data.doctor_id, data.right_bound_id, data.id))
            return DBHelper().update(sql, [data.get("id")])

    def select_by_id(id):
        """
        根据id查询数据
        :param id:
        :return:
        """
        if id is None:
            return

        sql = "select * from private_doctor_right_bound where id = %s"
        return DBHelper().selectone(sql, [id])

    def query_all(self, query_params):
        """
        查询所有数据
        :return:
        """
        if query_params is None:
            return

        sql = "select * from private_doctor_right_bound"
        sql += self.query_to_sql(query_params)
        return DBHelper().selectall(sql)

    def query_to_sql(self, query_params):
        """
        查询所有数据
        :return:
        """
        if query_params is None:
            return

        sql = " where"
        if query_params.userId is not None:
            sql += f" and  user_id = {query_params.userId} "
        if query_params.personId is not None:
            sql += f" and  person_id = {query_params.personId} "
        if query_params.groupId is not None:
            sql += f" and group_id = {query_params.groupId} "
        if query_params.weGroupId is not None:
            sql += f" and wx_group_id = {query_params.wxGroupId} "
        if query_params.scenarioCodes is not None:
            sql += " and scenario_code in ("
            for i in query_params.scenarioCodes:
                sql += f"{i},"
            sql += ")"

        return sql
